﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace Vehicle.Web.Pages.Pilot
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Pilot_Edit.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-08-30  Created by 杨光远 V1.00.0000
    /// 功能说明：编辑驾驶员信息页面类
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Pilot_Edit : Component.PageBase
    {
        #region ---基本变量实例化---
        Vehicle.BLL.Pilot b_Pilot = new Vehicle.BLL.Pilot();//定义逻辑层变量
        BLL.Log b_Log = new Vehicle.BLL.Log();
        Vehicle.Model.PilotInfo m_PilotInfo = new Vehicle.Model.PilotInfo();//定义实体层变量
        Vehicle.BLL.Vehicles b_Vehicles = new Vehicle.BLL.Vehicles();//定义逻辑层变量
        Vehicle.BLL.Motorcade b_Motorcade = new Vehicle.BLL.Motorcade();//定义逻辑层变量
        #endregion

        #region ---基本方法---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MotorcadeDataBind();//绑定车辆信息
            }
        }

        /// <summary>
        /// 初始绑定车队下拉数据
        /// </summary>
        public void MotorcadeDataBind()
        {
            b_Motorcade.DropDownListBind(drop_M_ID);//绑定车队

            VehiclesDataBind();
        }
        /// <summary>
        /// 根据车队下拉菜单绑定下属车辆
        /// </summary>
        public void VehiclesDropDataBind()
        {
            b_Vehicles.DropDownListBind(drop_V_ID, new Vehicle.Model.ComponentsInfo("Vehicles", "M_ID", drop_M_ID.SelectedValue, "P_ID", "0", "V_IsDelete", "1", "V_ID"));
        }
        /// <summary>
        /// 绑定车辆方法
        /// </summary>
        public void VehiclesDataBind()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {


                DataTable dt = b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Pilot", "P_ID", Request.QueryString["id"].ToString(), "P_ID"));
                if (dt.Rows.Count > 0)
                {
                    drop_M_ID.SelectedValue = dt.Rows[0]["M_ID"].ToString();//绑定车队下拉菜单
                    VehiclesDropDataBind();//根据车队绑定车辆;
                    drop_V_ID.SelectedValue = dt.Rows[0]["V_ID"].ToString();//绑定车辆菜单
                    txt_P_Num.Text = dt.Rows[0]["P_Num"].ToString();//绑定驾驶员编号
                    txt_P_Age.Text = dt.Rows[0]["P_Age"].ToString();//年龄
                    txt_P_CarAge.Text = dt.Rows[0]["P_CarAge"].ToString();//车龄
                    drop_P_Sex.SelectedValue = dt.Rows[0]["P_Sex"].ToString();//绑定性别
                    txt_P_License.Text = dt.Rows[0]["P_License"].ToString();//绑定驾照
                    txt_P_Name.Text = dt.Rows[0]["P_Name"].ToString();//绑定驾驶员名字
                    txt_P_Tel.Text = dt.Rows[0]["P_Tel"].ToString();//驾驶员电话
                    txt_P_Address.Text = dt.Rows[0]["P_Address"].ToString();//联系地址 
                    txt_P_CardID.Text = dt.Rows[0]["P_CardID"].ToString();//驾驶员身份证
                    drop_P_Transit.SelectedValue = dt.Rows[0]["P_Transit"].ToString();//是否有任务
                    txt_P_Note.Text = dt.Rows[0]["P_Note"].ToString();//备注
                    hf_P_LastTime.Value = dt.Rows[0]["P_LastTime"].ToString();//最后修改时间
                    hf_P_Num.Value = dt.Rows[0]["P_Num"].ToString();//绑定驾驶员编号
                    txt_P_Time.Text = dt.Rows[0]["P_Time"].ToString();//绑定驾驶员入录时间
                    hf_V_ID.Value = dt.Rows[0]["V_ID"].ToString();//绑定车辆ID
                    if (dt.Rows[0]["V_ID"].ToString() != "0")//判断是否退车
                    {
                        drop_V_ID.Enabled = false;
                        drop_M_ID.Enabled = false;
                        try
                        {
                            drop_V_ID.Items.Add(new ListItem(b_Vehicles.GetModel(new Vehicle.Model.ComponentsInfo("Vehicles", "V_ID", dt.Rows[0]["V_ID"].ToString(), "1", "1", "V_IsDelete", "1", "V_ID")).Rows[0]["V_LicenseNum"].ToString(), dt.Rows[0]["V_ID"].ToString()));
                            drop_V_ID.SelectedValue = dt.Rows[0]["V_ID"].ToString();  //默认选择当前的车辆
                        }
                        catch
                        {
                        }
                    }

                }
                else
                {
                    Common.MessageHelper.AlertBack("该记录已删除!");
                }
            }
            else
            {
                Common.MessageHelper.AlertBack("您的操作有误,请重新尝试!");
            }
        }
        /// <summary>
        /// 修改数据逻辑判断是否有数据更新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {
                if (hf_Set.Value == "1")//取值判断
                {
                    //提交前判断数据是否有被改变
                    if (b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Pilot", "P_ID", Request.QueryString["id"].ToString(), "P_ID")).Rows[0]["P_LastTime"].ToString() == hf_P_LastTime.Value)
                    {
                        Repeat();
                    }
                    else
                    {
                        d_Show.Visible = true;
                        hf_Set.Value = "0";
                    }
                }
                else
                {
                    Repeat();
                }
            }
            else
            {
                Common.MessageHelper.AlertBack("您的操作有误,请重新尝试!");
            }
        }
        /// <summary>
        /// 逻辑判断是否有编号重复
        /// </summary>
        public void Repeat()
        {
            //判断当前输入的驾驶员编号是否和之前的编号相同,不相同则先判断是否有重复的编号
            if (txt_P_Num.Text.Trim() != hf_P_Num.Value.ToString())
            {
                //添加前判断是否已经有该编号的驾驶员
                if (b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Pilot", "P_IsDelete", "0", "P_Num", txt_P_Num.Text.Trim(), "P_ID")).Rows.Count == 0)
                {
                    UpdateDate();
                }
                else
                {
                    Common.MessageHelper.Alert("已经有相同该编号的驾驶员,请重新填写");
                }
            }
            else
            {
                UpdateDate();
            }
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        protected void UpdateDate()
        {
            m_PilotInfo.P_ID = Common.TextHelper.StringToInt(Request.QueryString["id"].ToString());//驾驶员ID
            m_PilotInfo.M_ID = Common.TextHelper.StringToInt(drop_M_ID.SelectedValue);//所属车队ID
            m_PilotInfo.V_ID = Common.TextHelper.StringToInt(drop_V_ID.SelectedValue);//所属车辆
            m_PilotInfo.P_Num = txt_P_Num.Text.Trim();//驾驶员编号
            m_PilotInfo.P_Age = Common.TextHelper.StringToInt(txt_P_Age.Text.Trim());//驾驶员年龄
            m_PilotInfo.P_CarAge = Common.TextHelper.StringToInt(txt_P_CarAge.Text.Trim());// 驾驶员车龄
            m_PilotInfo.P_Sex = Common.TextHelper.StringToInt(drop_P_Sex.SelectedValue);//驾驶员性别
            m_PilotInfo.P_License = txt_P_License.Text.Trim();//驾驶员驾照
            m_PilotInfo.P_Name = txt_P_Name.Text.Trim();//驾驶员姓名
            m_PilotInfo.P_Tel = txt_P_Tel.Text.Trim();//联系电话
            m_PilotInfo.P_Address = txt_P_Address.Text.Trim();//联系地址
            m_PilotInfo.P_CardID = txt_P_CardID.Text.Trim();//驾驶员身份证
            m_PilotInfo.P_Transit = Common.TextHelper.StringToInt(drop_P_Transit.SelectedValue);//是否有任务
            m_PilotInfo.P_Note = txt_P_Note.Text.Trim();//驾驶员备注
            m_PilotInfo.P_LastTime = DateTime.Now;//最后修改时间 默认为当前时间
            m_PilotInfo.P_IsDelete = 0;//是否删除 默认0 则不删除
            m_PilotInfo.P_Time = Common.TextHelper.StringToDateTime(txt_P_Time.Text.Trim());//录入时间 默认为当前时间
            try
            {
                b_Pilot.Update(m_PilotInfo);

                if (hf_V_ID.Value != drop_V_ID.SelectedValue)
                {
                    b_Pilot.AllUpdate(new Vehicle.Model.ComponentsInfo("Vehicles", "P_ID", "0", "V_ID", hf_V_ID.Value));//更改原先车辆表
                    b_Pilot.AllUpdate(new Vehicle.Model.ComponentsInfo("Vehicles", "P_ID", Request.QueryString["id"].ToString(), "V_ID", drop_V_ID.SelectedValue));//更改车辆表

                }
                b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功修改编号为" + txt_P_Num.Text.Trim() + "的驾驶员信息"));//添加日志
                Common.MessageHelper.AlertRedirect("驾驶员信息修改成功", "Main.aspx");
            }
            catch
            {
                Common.MessageHelper.Alert("添加数据失败!");
            }
        }
        /// <summary>
        /// 车队下拉菜单绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void drop_M_ID_SelectedIndexChanged(object sender, EventArgs e)
        {
            VehiclesDropDataBind();
        }
        #endregion


    }
}
